<template>
  <div class="app-card">
    <div class="app-cover" @click="$emit('click', app)">
      <img v-if="app.cover" :src="app.cover" :alt="app.appName" />
      <div v-else class="default-cover">
        <div class="default-cover-content">
          <PlusOutlined style="font-size: 32px; color: #bbb" />
          <div class="default-cover-text">暂无封面</div>
          <div class="default-cover-hint">可在设置中上传</div>
        </div>
      </div>
      <!-- 悬浮时显示的操作按钮 -->
      <div class="hover-actions">
        <a-button v-if="showChatButton" type="primary" size="small" @click.stop="$emit('chat', app)">查看对话</a-button>
        <a-button size="small" @click.stop="$emit('detail', app)">查看详情</a-button>
        <a-button v-if="app.deployKey" size="small" @click.stop="$emit('view-work', app)">查看作品</a-button>
      </div>
    </div>
    <div class="app-info">
      <div v-if="showAuthor" class="app-info-content">
        <!-- 左侧：用户头像 -->
        <div class="user-avatar">
          <a-avatar :src="app.user?.userAvatar" :size="40">
            {{ app.user?.userName?.charAt(0) || 'U' }}
          </a-avatar>
        </div>
        <!-- 右侧：应用信息 -->
        <div class="app-details">
          <h3 class="app-title" @click="$emit('click', app)">{{ app.appName || '未命名应用' }}</h3>
          <p class="app-author">{{ app.user?.userName || 'NoCode 官方' }}</p>
          <!-- 标签显示 -->
          <div v-if="app.tags && app.tags.length > 0" class="app-tags">
            <a-tag v-for="tag in app.tags.slice(0, 3)" :key="tag" :color="getTagColor(tag)" size="small">
              {{ tag }}
            </a-tag>
            <a-tag v-if="app.tags.length > 3" size="small" style="color: #666">
              +{{ app.tags.length - 3 }}
            </a-tag>
          </div>
        </div>
      </div>
      <div v-else>
        <!-- 我的作品只显示应用标题，不显示头像和创建者信息 -->
        <h3 class="app-title" @click="$emit('click', app)">{{ app.appName || '未命名应用' }}</h3>
        <p class="app-meta">创建于 {{ formatDate(app.createTime) }}</p>
        <!-- 标签显示 -->
        <div v-if="app.tags && app.tags.length > 0" class="app-tags">
          <a-tag v-for="tag in app.tags.slice(0, 3)" :key="tag" :color="getTagColor(tag)" size="small">
            {{ tag }}
          </a-tag>
          <a-tag v-if="app.tags.length > 3" size="small" style="color: #666">
            +{{ app.tags.length - 3 }}
          </a-tag>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { PlusOutlined } from '@ant-design/icons-vue'
import dayjs from 'dayjs'

interface Props {
  app: any
  showAuthor?: boolean
  showChatButton?: boolean
}

interface Emits {
  (e: 'click', app: any): void
  (e: 'chat', app: any): void
  (e: 'detail', app: any): void
  (e: 'view-work', app: any): void
}

defineProps<Props>()
defineEmits<Emits>()

const formatDate = (date: string) => {
  return dayjs(date).format('MM-DD')
}

// 标签颜色映射
const getTagColor = (tag: string) => {
  const colorMap: Record<string, string> = {
    '网站': 'blue',
    '工具': 'green',
    '游戏': 'purple',
    '教育': 'orange',
    '商务': 'gold',
    'Vue': '#4fc08d',
    'React': '#61dafb',
    'HTML': '#e34c26',
    'CSS': '#264de4',
    'JavaScript': '#f7df1e',
    '管理系统': 'orange',
    '博客': 'cyan',
    '电商': 'magenta',
    '社交': 'lime',
    '简约': 'geekblue',
    '现代': 'volcano',
    '创意': 'pink',
    '专业': 'purple'
  }
  return colorMap[tag] || 'default'
}
</script>

<style scoped>
.app-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}

.app-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.app-cover {
  position: relative;
  height: 180px;
  overflow: hidden;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.default-cover {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: white;
}

.default-cover-content {
  text-align: center;
  opacity: 0.8;
}

.default-cover-text {
  font-size: 14px;
  margin-top: 8px;
  font-weight: 500;
}

.default-cover-hint {
  font-size: 12px;
  margin-top: 4px;
  opacity: 0.7;
}

.hover-actions {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.3s ease;
  flex-direction: column;
}

.app-card:hover .hover-actions {
  opacity: 1;
}

.app-cover::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.app-card:hover .app-cover::before {
  opacity: 1;
}

.app-info {
  padding: 16px;
}

.app-info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  flex-shrink: 0;
}

.app-details {
  flex: 1;
  min-width: 0;
}

.app-title {
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 4px 0;
  cursor: pointer;
  transition: color 0.2s ease;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-title:hover {
  color: #1890ff;
}

.app-author {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-meta {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
}

.app-tags {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
</style>
